<script type="text/javascript"><!--//--><![CDATA[//><!--
	
	var grid_reload = function() {
        $('#datagrid').treegrid('reload');
    };
	
	var f_status = function(val) {
        return (val == '1' ? '<img src="' + gs_path + '/content/images/ico_success.png" title="Publised" />' : '<img src="' + gs_path + '/content/images/delete.png" title="Unpublished" />');
    };
	
    $(document).ready(function() {
        $('#datagrid').treegrid({
            height: 298,
            nowrap: true,
            rownumbers: true,
            animate: true,
            //collapsible:true,
            idField: 'menu_id',
            treeField: 'menu_label',
            onBeforeLoad: function(row, param) {
                if (row) {
                    $(this).treegrid('options').url = gs_path + '/' + controller + '/treegrid_subdata';
                } else {
                    $(this).treegrid('options').url = gs_path + '/' + controller + '/treegrid_data';
                }
            }
        });

        // Set Action Button
        var buttons = [
                        // ----- >> Add Button
                        { label: 'Add', icon: 'add', onclick: function() {
                            var oSel = $('#datagrid').treegrid('getSelected'); 
                            
                            var $popup = openPopup({ title: '&nbsp;', 
                                width: 560,
                                height: 300,
                                url: gs_path + '/' + controller + '/create/' + (oSel == null ? '' : '?menu_id=' + oSel.menu_id),
                                buttons: [{
                                    text: 'Save',
                                    iconCls: 'icon-save',
                                    handler: function() {
                                        $('#form_popup').submitForm('/' + controller + '/ajax_post', { onComplete: function() { closePopup(); $('#datagrid').treegrid('reload'); } });
                                    }
                                }, {
                                    text: 'Close',
                                    iconCls: 'icon-cancel',
                                    handler: function() {
                                        closePopup();
                                    }
                                }],
                                onComplete: function(){ $('#form_popup').data('mode', 'create'); }
                             });
                            
                        }},
                        
                        // ----- >> Edit Button
					    { label: 'Edit', icon: 'edit', onclick: function() {
					        var oSel = $('#datagrid').treegrid('getSelected'); 
					        if (oSel == null) {
					            alert('Please select row!'); 
					        } else {
					            
					            var $popup = openPopup({ title: '&nbsp;',
					                width: 560,
					                height: 300,
					                url: gs_path + '/' + controller + '/edit/?menu_id=' + urlencode(oSel.menu_id),
					                buttons: [{
					                    text: 'Save',
					                    iconCls: 'icon-save',
					                    handler: function() {
					                        $('#form_popup').submitForm('/' + controller + '/ajax_post', { onComplete: function() { closePopup(); $('#datagrid').treegrid('reload'); } });
					                    }
					                }, {
					                    text: 'Close',
					                    iconCls: 'icon-cancel',
					                    handler: function() {
					                        closePopup();
					                    }
                                    }],
					                    onComplete: function() { $('#form_popup').data('mode', 'edit'); }
					            });
					        } 
					    }},

					    // ----- >> Delete Button
						{label: 'Delete', icon: 'delete', onclick: function() { var oSel = $('#datagrid').treegrid('getSelected'); if (oSel == null) { alert('Please select row!'); } else { deleteData('?menu_id=' + oSel.menu_id); } } },
                        
						// ----- >> Export Button
						{label: 'Export', icon:'menu', onclick: function() { 
							setLoading(true);
							$.ajax({
								type: 'POST', url: gs_path+'/'+controller+'/export/', cache: false, data: jQuery.param( $('#fm_search').serializeArray().concat({ name:'sessid', value:sessid })),
								success: function(data) { setLoading(false); if(data!=''){ alert(data); return false; } document.getElementById('if_download').src='<?php echo base_url(); ?>app/download/?file=<?php echo _SESSID; ?>.json&filename=<?php echo strtolower(APP); ?>_menu__NOW_.json'; },
								error: function(xhr, ajaxOptions, thrownError) { setLoading(false); var ls_message = ''; var ls_logmessage = ''; if (xhr.status == 0) { ls_message = 'You are offline!!\n Please Check Your Network.'; } else if (xhr.status == 404) { ls_message = 'Requested URL not found.'; } else if (xhr.status == 500) { ls_message = 'Internal Server Error.';  } else if (thrownError == 'parsererror') { ls_message = 'Error.\nParsing JSON Request failed.' } else if (thrownError == 'timeout') { ls_message = 'Request Time out.'; } else { ls_message = 'Unknow Error.\n' + xhr.responseText; } $.messager.alert('&nbsp;',nl2br(ls_message),'error'); }, dataType: "html"
							});
						}},
						
						// ----- >> Import Button
                        { label: 'Import', icon: 'menu', onclick: function() {
                            var oSel = $('#datagrid').datagrid('getSelected'); 
                            
                            var $popup = openPopup({ title: '&nbsp;', 
                                width: 700,
                                height: 395,
                                url: gs_path + '/' + controller + '/form_import/'
                             });
                            
                        }},
						
                        // ----- >> Expand Button
                        {label: 'Expand', icon: 'down', onclick: function() {

                            var node = $('#datagrid').treegrid('getSelected');
						    if (node) {
						        $('#datagrid').treegrid('expandAll', node.code);
						    } else {
						        $('#datagrid').treegrid('expandAll');
						    }
						}},

						// ----- >> Collapse Button
                        {label: 'Collapse', icon: 'up', onclick: function() {

                            var node = $('#datagrid').treegrid('getSelected');
                            if (node) {
                                $('#datagrid').treegrid('collapseAll', node.code);
                            } else {
                                $('#datagrid').treegrid('collapseAll');
                            }
                        } }];
        setAction(buttons);

    });
//--><!]]></script>

<table id="datagrid">
<thead frozen="true">
    <tr>
        <th field="parent_menu_id" width="60" hidden="true">Parent Menu Id</th>
        <th field="menu_label" width="350">Label</th>
    </tr>
</thead>
<thead>
    <tr>
        <th field="url" width="250">URL</th>
        <th field="menu_id" width="90" align="center">Menu Id</th>
        <th field="order_num" width="70" align="center">No.Urut</th>
        <th field="status" width="70" align="center" formatter="f_status">Status</th>
    </tr>
</thead>
</table>
